//
//  UIViewController+Utils.m
//  YZStock
//
//  Created by wangminhong on 15/9/17.
//  Copyright (c) 2015年 cqjr. All rights reserved.
//

#import "UIViewController+Utils.h"
#import <Masonry/Masonry.h>
#import <objc/runtime.h>
#import "MBProgressHUD+Utils.h"


static char bottomHelperViewKey;
static char backItemKey;

@implementation UIViewController (Utils)

@dynamic bottomHelperView;

@dynamic backItem;

+ (void)load{
    Method originMethod = class_getInstanceMethod([self class], @selector(viewDidLoad));
    Method newMethod = class_getInstanceMethod([self class], @selector(viewWillAppearNew:));
    method_exchangeImplementations(originMethod, newMethod);
    
    Method originMoveMethod = class_getInstanceMethod([self class], @selector(didMoveToParentViewController:));
    Method newMoveMethod = class_getInstanceMethod([self class], @selector(didMoveToParentViewControllerNew:));
    method_exchangeImplementations(originMoveMethod, newMoveMethod);
}

- (void)viewWillAppearNew:(BOOL)animated{
    [self viewWillAppearNew:animated];
    if (iPhoneX) {
        self.navigationController.delegate = self;
    }
    if (self.backItem == nil) {
        self.backItem = [[UIBarButtonItem alloc] init];
    }
    self.backItem.title = @"";
    self.navigationItem.backBarButtonItem = self.backItem;
    for ( id iv in self.navigationController.navigationBar.subviews) {
            if ([iv isKindOfClass:NSClassFromString(@"_UINavigationBarBackIndicatorView")]){
            UIView *vi = iv;
            if (vi) {
                vi.frame = CGRectMake(0, 0, 44, 44);
            }
                break;
        }
    }
}

- (void)didMoveToParentViewControllerNew:(UIViewController *)parent{
    if (parent == nil) {
        if (iPhoneX) {
            self.navigationController.delegate = nil;
        }
    }
}

- (void)setBackItem:(UIBarButtonItem *)backItem{
    objc_setAssociatedObject(self, &backItemKey, backItem, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIBarButtonItem *)backItem{
   return  objc_getAssociatedObject(self, &backItemKey);
}

- (UIView *)bottomHelperView{
    
    return objc_getAssociatedObject(self, &bottomHelperViewKey);
}

- (void)setBottomHelperView:(UIView *)bottomHelperView{
    
    objc_setAssociatedObject(self, &bottomHelperViewKey, bottomHelperView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)registerForKeyboardAutoWithBottomView:(UIView *)bottomView{
    
    [self registerForKeyboardAutoWithBottomView:bottomView andGestureFlag:YES];
}

- (void)registerForKeyboardAutoWithBottomView:(UIView *)bottomView andGestureFlag:(BOOL)gestureFlag{
    
    self.bottomHelperView = bottomView;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHide:) name:UIKeyboardWillHideNotification object:nil];
    
    if (gestureFlag) {
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignFirstResponderAll)];
        [self.view addGestureRecognizer:tapGesture];
    }
}

- (void)resignFirstResponderAll{
    
    [self.view endEditing:YES];
}

- (void)keyboardWasShow:(NSNotification *)notification{
    
    NSDictionary *info = [notification userInfo];
    
    NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    
    [self.bottomHelperView mas_updateConstraints:^(MASConstraintMaker *maker){
        
        maker.height.mas_equalTo(keyboardSize.height);
    }];
    
    [UIView animateWithDuration:[[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue] animations:^{
        
        [self.view layoutIfNeeded];
    }];
    
}

- (void)keyboardWasHide:(NSNotification *)notification{
    
    NSDictionary *info = [notification userInfo];
    
    [self.bottomHelperView mas_updateConstraints:^(MASConstraintMaker *maker){
        
        maker.height.mas_equalTo(0);
    }];
    
    [UIView animateWithDuration:[[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue] animations:^{
        
        [self.view layoutIfNeeded];
    }];
}

#pragma mark- UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (iPhoneX) {
        CGRect fra = self.tabBarController.tabBar.frame;
        if (fra.origin.y < ScreenHeight -  83) {
            fra.origin.y = ScreenHeight - 83;
            self.tabBarController.tabBar.frame = fra;
            UIOffset offset = UIOffsetMake(0, -5);
            self.tabBarController.tabBarItem.titlePositionAdjustment = offset;
        }
    }
}


@end
